package com.android.kmlmrg.services;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;


import com.android.kmlmrg.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class LocationServiceManager implements LocationListener  {

	/**LOG**/
	private static final  String LOG_TAG = LocationServiceManager.class.getName();
	
	private Activity activity;
	
	private MapView mapView;

	private MapController mapController;
	
	private LocationManager locationManager;

	private Location  location;
	
	private double latitude;
	
	private double longitude;
	
	private double altitude;

	private float accuracy;

	public LocationServiceManager ( Activity pActivity ) {
		Log.i(LOG_TAG, "START - Construct LocationServiceManager");
		activity = pActivity;		
		init();
		Log.i(LOG_TAG, "START - Construct LocationServiceManager");
	}
	
	private void init () {
		Log.i(LOG_TAG, "START - init");
		
		mapView = (MapView) activity.findViewById(R.id.mapview);
		Log.i(LOG_TAG, "mapView - " + mapView );
		mapController = mapView.getController();
		Log.i(LOG_TAG, "mapController - " + mapController );
		activity.getApplicationContext();
		String svcName= Context.LOCATION_SERVICE ;
		locationManager = (LocationManager)activity.getApplicationContext().getSystemService(svcName);
		Log.i(LOG_TAG, "locationManager - " + locationManager );
		Log.i(LOG_TAG, "END - init");
	}
	
	
	public void defaultParametterMapView () {
		Log.i(LOG_TAG, "START - defaultParametterMapView");
		mapView.setBuiltInZoomControls(true);		
		mapView.setSatellite(false);
		mapView.setBuiltInZoomControls(true);
		mapView.setClickable(true);
	
		mapController.setZoom(17);
		
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setSpeedRequired(false);
		criteria.setCostAllowed(true);
		String provider = locationManager.getBestProvider(criteria, true);
		location = locationManager.getLastKnownLocation(provider);
		locationManager.requestLocationUpdates(provider, 2000, 10,
				this);
		
		Log.i(LOG_TAG, "START - defaultParametterMapView");
	}
	
	public void activateMapLocation () {
		init();
	}
	
	public void disableMapLocation () {
		locationManager.removeUpdates(this);
	}
	
	public MapView getMapView() {
		return mapView;
	}

	public void setMapView(MapView mapView) {		
		this.mapView = mapView;
		init();

	}
	
	
	public MapController getMapController() {
		return mapController;
	}


	public LocationManager getLocationManager() {
		return locationManager;
	}

	public void onLocationChanged(Location arg0) {
		latitude = location.getLatitude();
		longitude = location.getLongitude();
		altitude = location.getAltitude();
		accuracy = location.getAccuracy();
		GeoPoint vGeoPoint = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
		mapController.animateTo(vGeoPoint);
		mapController.setCenter(vGeoPoint);
		
		
	}

	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public double getLatitude() {
		return latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public double getAltitude() {
		return altitude;
	}

	public float getAccuracy() {
		return accuracy;
	}





	






}
